package com.xiaoxu.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.rental.entity.Role;
import com.xiaoxu.rental.service.PermissionService;
import com.xiaoxu.rental.service.RoleService;
import com.xiaoxu.rental.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**

 * @author xiaoxu
 * @since 2024-07-24
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/rental/role")
@SuppressWarnings("all")
public class RoleController {


    @Resource
    private RoleService roleService;

    @Resource
    private PermissionService permissionService;

    @ApiOperation(value = "查询角色列表")
    @PostMapping("{start}/{size}")
    public Result search(@PathVariable Integer start,
                         @PathVariable Integer size,
                         @RequestBody Role role){
        Page<Role> page=new Page<>(start,size);
        return Result.success(roleService.selectList(page,role));
    }
    //两点：1. @PreAuthorize("hasAuthority('sys:role:add')")：表示只有具有"sys:role:add"权限的用户才能访问该接口。
    // 2. @RequestBody Role role：表示该接口接收一个JSON格式的Role对象作为请求体。
    @PreAuthorize("hasAuthority('sys:role:add')")
    @PostMapping("/save")
    public Result save(@RequestBody Role role){
        return roleService.save(role)?Result.success():Result.fail();
    }

    @PreAuthorize("hasAuthority('sys:role:edit')")
    @PutMapping("/update")
    public Result update(@RequestBody Role role){
        role.setUpdateTime(LocalDateTime.now());
        return roleService.updateById(role)?Result.success():Result.fail();
    }

    @ApiOperation(value = "是否有用户")
    @GetMapping("hasUser/{id}")
    public Result hasUser(@PathVariable Integer id){
        return  roleService.hasUser(id)?Result.success().setMessage("有"):
                Result.success().setMessage("无");
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids)
    {
        return roleService.delete(ids)?Result.success():Result.fail();
    }
    //构建树形结果
    @GetMapping("/permissionTree")
    public Result selectPermissionTree(Integer userId, Integer roleId){

        return Result.success(permissionService.selectPermissionTree(userId, roleId));
    }
    @PreAuthorize("hasAuthority('sys:role:assign')")
    @GetMapping("{roleId}/{permissionIds}")
    public Result assignPermission(@PathVariable int roleId,
                                   @PathVariable String permissionIds) {
        List<Integer> list = Arrays.stream(permissionIds.split(","))
                .map(Integer::parseInt)
                .toList();
        return roleService.assignPermission(roleId, list) ?
                Result.success() : Result.fail();
    }

    @GetMapping
    public Result list (){
        return Result.success().setData(roleService.list()) ;
    }



}
